const fs = require('fs').promises
const path = require('path')
function* read() {
    let a = yield fs.readFile('./name.txt', 'utf8', () => { })
    let b = yield fs.readFile(a, 'utf8', () => { })
    return b
}
/**
 * @param {*} handle 将generator方法 简化
 * @returns Promise
 * 内部使用递归 将上一次next返回的value 传递给下一个next的参数
 * 如果yield结果非promise用promise.resolve包裹
 */
function co(handle) {
    return new Promise((resolve, reject) => {
        function next(data) {
            const { value, done } = handle.next(data);
            if (done) {
                return resolve(data)
            }
            // 为确包yield 为promise 用Promise.resolve包一层
            Promise.resolve(value).then(e => {
                next(e)
            }, reject)
        }
        next()
    })
}

co(read()).then(res => {
    console.log(res)
})

